package com.leap.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import static com.baomidou.mybatisplus.core.toolkit.Constants.MYBATIS_PLUS;
import static com.baomidou.mybatisplus.core.toolkit.StringPool.UNDERSCORE;
import com.leap.entity.Article;
import com.leap.entity.vo.HistoryVO;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.annotations.Param;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface ArticleMapper extends BaseMapper<Article> {

    @Insert("INSERT INTO  article_media(article_id, media_id, sort_order, is_cover) VALUES( #{articleId}, #{mediaId}, #{sort}, #{cover} ) ")
    int insertArticleMediaDTO(Long articleId,Long mediaId,int sort,int cover);

    @Select("SELECT COUNT(*) FROM search_history WHERE search_content LIKE CONCAT('%', #{title}, '%') AND account_id = #{accountId}")
    int selectSearchHistory(String title,Long accountId);

    @Insert("INSERT INTO search_history(account_id, search_content,update_time) VALUES (#{accountId}, #{title},#{now})")
    int saveSearchHistory(String title, Long accountId, LocalDateTime now);

    @Update("UPDATE search_history SET account_id = #{accountId}, search_content = #{title}, update_time = #{now}  WHERE account_id = #{accountId} AND search_content = #{title}")
    int updateSearchHistory(String title,Long accountId, LocalDateTime now);

    @Select("SELECT id,account_id, search_content FROM search_history WHERE account_id = #{id}")
    List<HistoryVO> selectHistoryById(Long id);

    @Select("SELECT category_id FROM article WHERE id = #{id}")
    int selectCategoryIdByArticleId(@Param("id") Long id);

    @Select("SELECT COUNT(*) FROM article WHERE is_video = 0 AND is_publish = 1")
    int getArticleSum();

    @Select("SELECT COUNT(*) FROM article WHERE is_video = 1 AND is_publish = 1")
    int getVideoArticleSum();

    @Select("SELECT COUNT(*) FROM article WHERE is_video = 0 AND is_publish = 0")
    int getIsNotPublishSum();

    @Select("SELECT COUNT(*) FROM article WHERE is_video = 1 AND is_publish = 0")
    int getIsNotPublishVideoSum();

    @Select("SELECT COUNT(*) FROM article WHERE category_id = #{category} AND is_publish = 1 AND is_video = #{isVideo}")
    int getCategoryArticleSum(int category,int isVideo);

    @Select("SELECT COUNT(*) FROM article WHERE category_id = #{category} AND is_publish = 0 AND is_video = #{isVideo}")
    int getNotPublishCategoryArticleSum(int category,int isVideo);

    @Update("UPDATE article SET browse_num = browse_num + 1 WHERE id = #{articleId}")
    void addOneBrowseNum(Long articleId);

    @Update("UPDATE article SET like_num = like_num + 1 WHERE id = #{articleId}")
    void addOneLikeNum(Long articleId);

    @Update("UPDATE article SET like_num = like_num - 1 WHERE id = #{articleId}")
    void minusOneLikeNum(Long articleId);
}
